package com.gxa.community.admin.controller.goods;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.community.admin.mapper.goods.GoodsTagMapper;
import com.gxa.community.admin.service.goods.impl.GoodsTagServiceImpl;
import com.gxa.community.core.pojo.domain.goodsdo.GoodsTag;
import com.gxa.community.core.utils.R;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;


/**
 * <p>
 * 商品标签 前端控制器
 * </p>
 *
 * @author gxa
 * @since 2022-07-18
 */
@RestController
@Slf4j
@Validated
@RequestMapping("/goods/tag")
public class GoodsTagController {
    @Autowired
    GoodsTagServiceImpl goodsTagService;
    /**
     * 添加标签
     * @param goodsTag
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody GoodsTag goodsTag) {
        boolean b = goodsTagService.saveOrUpdate(goodsTag);
        if (b){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 查询并分页
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/query")
    public R query(@NotNull(message = "page不能为空") Integer page,
                   @NotNull(message = "size不能为空") Integer size) {
        Page page1 = new Page<>(page,size);
        Page page2 = goodsTagService.page(page1,null);
        return R.ok().put("data",page2.getRecords()).put("cont",page2.getTotal());
    }
    /**
     * 修改标签
     * @param goodsTag
     * @return
     */
    @PutMapping("/edit")
    public R edit(@RequestBody @Validated GoodsTag goodsTag) {
        boolean b = goodsTagService.updateById(goodsTag);
        if (b){
            return R.ok();
        }
        return R.error();
    }
    /**
     * 批量删除
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public R del(@NotNull(message = "id不能为空") Integer... id) {
        Integer count = 0;
        for (Integer integer :id) {
            boolean b = goodsTagService.removeById(integer);
            if (b){
                count++;
            }
        }
        return R.ok("成功删除了:"+count+"个");
    }

    /**
     * 根据标签姓名查询
     * @param slt
     * @return
     */
    @GetMapping("/seek")
    public R seek(@NotEmpty(message = "搜索内容不能为空") String slt, @NotNull(message = "page不能为空") Integer page, @NotNull(message = "sizs不能为空") Integer size) {
        QueryWrapper<GoodsTag> wrapper1 = new QueryWrapper<>();
        log.info("标签接口:搜索拿到的{}==========",slt);
        Page page1 = new Page<>(page,size);
        wrapper1.eq("name",slt);
        Page page2 = goodsTagService.page(page1,wrapper1);
        //把分页完成的数据和总共的数量返回
        return R.ok().put("data",page1.getRecords()).put("cont",page2.getTotal());

    }
    /**
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/editStatus")
    public R editStatus(@NotNull(message = "id不能为空") Integer id,@NotNull(message = "状态码不能为空") Integer status) {
        UpdateWrapper wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("status",status);
        boolean b = goodsTagService.update(null, wrapper);
        if (b){
            return R.ok();
        }
        return R.error();
    }

}

